using System.Collections.Generic;
using Headspring.Training.Core.Model;
using Headspring.Training.Core.Model.StateCommands;

namespace Headspring.Training.Core.Services.Impl
{
	public class WorkflowFacilitator : IWorkflowFacilitator
	{
		private readonly ICalendar _calendar;
		private readonly IEmailService _emailService;

		public WorkflowFacilitator(ICalendar calendar, IEmailService emailService)
		{
			_calendar = calendar;
			_emailService = emailService;
		}

		public IStateCommand[] GetValidStateCommands(WorkOrder workOrder,
		                                             Employee currentUser)
		{
			var commands =
				new List<IStateCommand>(GetAllStateCommands(workOrder, currentUser));
			commands.RemoveAll(delegate(IStateCommand obj) { return !obj.IsValid(); });

			return commands.ToArray();
		}

		public virtual IStateCommand[] GetAllStateCommands(WorkOrder workOrder,
		                                                   Employee currentUser)
		{
			var commands = new List<IStateCommand>();
			commands.Add(new SaveDraftCommand(workOrder, currentUser));
			commands.Add(new DraftToAssignedCommand(workOrder, currentUser, _calendar));
			commands.Add(new AssignedToInProgressCommand(workOrder, currentUser));
			commands.Add(new AssignedToDraftCommand(workOrder, currentUser, _emailService,
			                                        _calendar));
			commands.Add(new InProgressToAssignedCommand(workOrder, currentUser));
			commands.Add(new InProgressToCompleteCommand(workOrder, currentUser));
			commands.Add(new CompleteToAssignedCommand(workOrder, currentUser));
			commands.Add(new DraftToCancelledCommand(workOrder, currentUser));
			commands.Add(new AssignedToCancelledCommand(workOrder, currentUser));
			commands.Add(new InProgressToCancelledCommand(workOrder, currentUser));
			commands.Add(new CompleteToCancelledCommand(workOrder, currentUser));

			return commands.ToArray();
		}
	}
}